using System;
using System.Collections.Generic;
using System.Text;
using GL.GL.Classes;
using System.Windows.Forms;

namespace HR.Controls
{
    public class LookupTextBox:ExtendedTextBox
    {
        ToolTip tt = new ToolTip();

        private string m_sql = "";
        private string m_returncolumn = "";
        private string m_sqldescription = "";
        private string m_lookup_column = "";

        private string m_title = "List";

        private bool m_isautocheckvalidation = true;


        public LookupParameterTextBox  Parameter
        {
            set{
                if (value != null)
                {
                    m_sql = value.LookupParam.Sql;
                    m_returncolumn = value.LookupParam.ReturnColumn;
                    m_sqldescription = value.SqlDescription;
                    m_lookup_column = value.LookupParam.LookupColumn;
                    m_title = value.LookupParam.Title;
                }
                else
                {
                    m_sql = "";
                    m_returncolumn = "";
                    m_sqldescription = "";
                    m_lookup_column = "";
                    m_title = "";
                }
            }
        }


        public bool IsAutoCheckValidation { get { return m_isautocheckvalidation; } set { m_isautocheckvalidation = value; } }
        public string LookupColumn { get { return m_lookup_column; } set { m_lookup_column = value; } }
        public string Sql { set { m_sql = value; } get { return m_sql; } }
        public string ReturnColumn { set { m_returncolumn = value;} get { return m_returncolumn; } }
        public string SqlDescription { set { m_sqldescription = value; } get { return m_sqldescription; } }
        public string Title { set { m_title = value; } get { return m_title; } }
        public string Description
        {
            get
            {
                if (m_sqldescription != "")
                {
                    string csql = m_sqldescription.Replace("@TEXT", Constants.sqlStr(Text));
                    object s = Program.db.executeFirstRowFirstCol(csql);
                    if (s != null)
                        return s.ToString();
                    else
                        return "";
                }
                else
                    return "";
            }
        }

        public LookupTextBox()
            : base()
        {
            this.BackColor = System.Drawing.Color.PaleGoldenrod;
            //tt.Active = true;
            tt.AutomaticDelay = 1;
            tt.ReshowDelay = 1;
            tt.InitialDelay = 1;
            tt.AutoPopDelay = 1;
            tt.ShowAlways = false;
            tt.IsBalloon = true;
            tt.ToolTipIcon = ToolTipIcon.Error;
            tt.ToolTipTitle = "Error";
        }

        protected override void OnKeyDown(System.Windows.Forms.KeyEventArgs e)
        {
            if (Enabled && !ReadOnly && m_sql!="")
            {
                if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.F5)
                {
                    if (Text.Trim() == "" || e.Control || e.KeyCode == Keys.F5)
                    {
                        string s=(string)Constants.Lookup(null, m_sql, m_returncolumn, "< ERROR >",m_lookup_column,m_title);
                        if (s != "< ERROR >")
                        {
                            Text = s;
                            if (e.KeyCode == Keys.F5)
                                SendKeys.Send("{ENTER}");
                        }
                        else
                        {
                            this.Focus();
                            e.Handled = true;
                            return;
                        }
                    }
                }
            }

            base.OnKeyDown(e);

        }

        protected override void OnLostFocus(EventArgs e)
        {
            if (m_isautocheckvalidation)
            {
                if (Program.fMain.ActiveMdiChild == this.FindForm())
                {
                    if (!isValid())
                    {
                        this.Focus();
                        this.BackColor = System.Drawing.Color.LightSalmon;
                        tt.Show("Data tidak valid", this);
                        tt.Show("Data tidak valid", this);                        
                        return;
                    }
                    else
                    {

                        tt.Hide(this);
                        this.BackColor = System.Drawing.Color.PaleGoldenrod;
                    }
                }
                else
                {
                    tt.Hide(this);
                }
            }
                        
            base.OnLostFocus(e);
        
        }

        public bool isValid()
        {
            if (m_sqldescription != "")
            {
                string csql = m_sqldescription.Replace("@TEXT", Constants.sqlStr(Text));
                object s = Program.db.executeFirstRowFirstCol(csql);
                if (s == null)
                {
                    
                    return false;
                }
                
            }            
            return true;
        }

        protected override void Dispose(bool disposing)
        {
            tt.Dispose();
            base.Dispose(disposing);
        }




            
    }
}
